import 'package:fish_redux/fish_redux.dart';
// import 'package:flutter_blue_card_plugin/flutter_blue_card_plugin.dart'
//     hide ResultData;
import 'package:kunlun_gas/app_files.dart';

import 'action.dart';
import 'state.dart';

Effect<BindBlueToothViewState> buildEffect() {
  return combineEffects(<Object, Effect<BindBlueToothViewState>>{
    BindBlueToothViewAction.action: _onAction,
    BindBlueToothViewAction.onSubmit: _onSubmit,
  });
}

void _onAction(Action action, Context<BindBlueToothViewState> ctx) {}

void _onSubmit(Action action, Context<BindBlueToothViewState> ctx) async {
  if (ctx.state.numberController.text.length == 0) {
    CommonUtils.showErrorInfo(ctx.context, '请输入读卡器设备号');
    return;
  }
  int ret = await CommonUtils.showOKCanceltView(
      ctx.context, '一户只能绑定一个蓝牙读卡器，请确认读卡器编号输入正确',
      title: ctx.state.numberController.text);
  if (ret != 1) {
    return;
  }
  // BlueCardResultData resultData = await FlutterBlueCardPlugin.checkBluetoothSn(
  //     ctx.state.numberController.text);
  // if (resultData.code != 0) {
  //   await CommonUtils.showAlertView(ctx.context, resultData.desc);
  //   return;
  // }
  ResultData res = await CommonNetWork.bindBluetooth(
      ctx.state.userID, ctx.state.numberController.text);
  if (res.success) {
    await CommonUtils.showAlertView(ctx.context, '绑定成功');
    bus.emit('bindSuccess', ctx.state.numberController.text);
    Navigator.pop(ctx.context);
  } else {
    CommonUtils.showErrorInfo(ctx.context, res.desc);
  }
}
